package com.achao.view;

import com.achao.dao.IdentificationDao;
import com.achao.dao.TravellerDao;
import com.achao.service.PassengerService;
import com.achao.service.impl.PassengerServiceImpl;
import com.achao.utils.MybatisUtils;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

@Log4j2
@Data
public class UserRegView extends JFrame {

    private static UserRegView rv;

    public static UserRegView getInstance() {
        if (rv == null) {
            rv = new UserRegView();
        }
        return rv;
    }

    private JLabel usernameLabel = new JLabel("用户名：");
    private JLabel passwordLabel = new JLabel("密  码：");
    private JLabel password2Label = new JLabel("确认密码：");
    private JLabel identificationTypeLabel = new JLabel("证件类型");
    private JLabel nameLabel = new JLabel("姓名：");
    private JLabel identificationNumLabel = new JLabel("证件号码");
    private JLabel phoneLabel = new JLabel("手机号码");
    private JLabel emailLabel = new JLabel("电子邮箱");
    private JLabel travellerTypeLabel = new JLabel("旅客类型");

    private JButton submitBtn = new JButton("立即注册");
    private JButton backBtn = new JButton("返回登录");
    private JTextField usernameText = new JTextField();// 输入账号
    private JTextField nameText = new JTextField();// 输入名字

    private JPasswordField passwordText = new JPasswordField();// 输入密码
    private JPasswordField password2Text = new JPasswordField();// 确认密码
    private JTextField phoneText = new JTextField();    // 输入手机
    private JTextField emailText = new JTextField();    // 输入邮箱
    private JTextField identificationNumText = new JTextField();// 输入证件码

    private ButtonGroup identificationType = new ButtonGroup(); //
    private JComboBox<String> travellerType = new JComboBox<>(); // 旅客类型下拉框

    private List<JRadioButton> identificationTypeList = new ArrayList<>();

    private UserRegView() {
        setSize(350, 500);
        setLayout(null);
        setResizable(false);
        setDefaultCloseOperation(0);
        setLocationRelativeTo(null);

        init();
        action();

        initIdentification();
        initTravellerType();

        this.setVisible(true);
    }

    private void init() {
        usernameLabel.setBounds(20, 20, 70, 30);
        add(usernameLabel);

        usernameText.setBounds(100, 20, 200, 30);
        add(usernameText);


        passwordLabel.setBounds(20, 50, 70, 30);
        add(passwordLabel);

        passwordText.setBounds(100, 50, 200, 30);
        add(passwordText);

        password2Label.setBounds(20, 80, 70, 30);
        add(password2Label);

        password2Text.setBounds(100, 80, 200, 30);
        add(password2Text);

        identificationTypeLabel.setBounds(20, 110, 70, 30);
        add(identificationTypeLabel);

        nameLabel.setBounds(20, 230, 70, 30);
        add(nameLabel);

        nameText.setBounds(100, 230, 200, 30);
        add(nameText);

        identificationNumLabel.setBounds(20, 260, 70, 30);
        add(identificationNumLabel);

        identificationNumText.setBounds(100, 260, 200, 30);
        add(identificationNumText);

        phoneLabel.setBounds(20, 290, 70, 30);
        add(phoneLabel);

        phoneText.setBounds(100, 290, 200, 30);
        add(phoneText);


        emailLabel.setBounds(20, 320, 70, 30);
        add(emailLabel);

        emailText.setBounds(100, 320, 200, 30);
        add(emailText);

        travellerTypeLabel.setBounds(20, 350, 70, 30);
        add(travellerTypeLabel);

        travellerType.setBounds(100, 350, 200, 30);
        add(travellerType);

        submitBtn.setBounds(50, 400, 100, 30);
        add(submitBtn);

        backBtn.setBounds(200, 400, 100, 30);
        add(backBtn);

    }

    private void action() {
        submitBtn.addActionListener(e -> doSubmit());
        backBtn.addActionListener(e -> {
            this.setVisible(false);
            UserLoginView.getInstance().setVisible(true);
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int key = JOptionPane.showConfirmDialog(null, "是否关闭窗口?", "提示", JOptionPane.OK_OPTION);
                if (key==0){
                    System.exit(0);
                }
            }
        });
    }

    private void initIdentification() {
        System.out.println("初始化证件类型数据");
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        IdentificationDao mapper = sqlSession.getMapper(IdentificationDao.class);
        List<String> allType = mapper.getAllType();
        log.debug(allType);
        for (int i = 0; i < allType.size(); i++) {
            String s = allType.get(i);
            JRadioButton jRadioButton = new JRadioButton(s);
            identificationType.add(jRadioButton);
            int a = i * 25;
            jRadioButton.setBounds(100, 110 + a, 200, 30);
            add(jRadioButton);
            identificationTypeList.add(jRadioButton);
        }
        if (identificationTypeList.size() != 0) {
            JRadioButton jRadioButton = identificationTypeList.get(0);
            jRadioButton.setSelected(true);
        }

    }

    private void initTravellerType() {
        System.out.println("初始化乘客类型");
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TravellerDao mapper = sqlSession.getMapper(TravellerDao.class);
        List<String> allType = mapper.getAllType();
        for (String s : allType) {
            travellerType.addItem(s);
        }

    }

    private void doSubmit() {
        System.out.println("点击了立即注册");
        PassengerService passengerService = new PassengerServiceImpl();
        passengerService.frontRegister();
    }
}
